package com.wcgksoft.controller.Patrol;

import com.wcgksoft.common.util.Layui;
import com.wcgksoft.common.util.MsgInfo;
import com.wcgksoft.entity.Patrol.PatrolAccount;
import com.wcgksoft.entity.system.EnterpriseProject;
import com.wcgksoft.entity.system.SysUser;
import com.wcgksoft.service.Patrol.PatrolAccountService;
import org.apache.shiro.SecurityUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

/**
 * @author 御风
 * @version V1.0
 * @Package com.wcgksoft.controller.Patrol
 * @Description TODO 施工队主页显示数据
 * @date 2020/4/2 14:27
 * @ClassName CountStatisticsController
 */
@Controller
@RequestMapping("system/count")
public class CountStatisticsController {
    @Autowired
    private PatrolAccountService patrolAccountService;

    /**
     * unComplete count
     * @return
     */
    @ResponseBody
    @RequestMapping("patrolUnComplete")
    public Layui countStatic(){
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        EnterpriseProject enterpriseProject = (EnterpriseProject) SecurityUtils.getSubject().getSession().getAttribute("area");
        List list5 = patrolAccountService.getHibernateSession().createSQLQuery("select COUNT(*) from t_patrolaccount where status='2' and proId='" + sysUser.getProId() + "' and enterpriseId='" + sysUser.getEnterprise().getId() + "' and receiverId='" + sysUser.getId() + "' and areaId='" + enterpriseProject.getId() + "' and DATE_FORMAT(completeTime,'%Y')=DATE_FORMAT(CURDATE(),'%Y');").list();
        return Layui.data(0, "查询成功", list5.get(0));
    }

    /**
     * patrol by month
     * @return
     */
    @ResponseBody
    @RequestMapping("patrolCompleteByMonth")
    public MsgInfo patrolCompleteByMonth(){
        MsgInfo msgInfo = new MsgInfo();
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        EnterpriseProject enterpriseProject = (EnterpriseProject) SecurityUtils.getSubject().getSession().getAttribute("area");
        List<Object[]> list6 = patrolAccountService.getHibernateSession().createSQLQuery("" +
                "SELECT CASE datas.weeks WHEN '1' THEN '1'  " +
                "WHEN '2' THEN '2' WHEN '3' THEN '3' WHEN '4' THEN '4'  " +
                "WHEN '5' THEN '5' WHEN '6' THEN '6' WHEN '7' THEN '7'  " +
                "WHEN '8' THEN '8'  " +
                "WHEN '9' THEN '9'  " +
                "  WHEN '10' THEN " +
                "  '10'  " +
                "  WHEN '11' THEN " +
                "  '11'  " +
                "  WHEN '12' THEN " +
                "  '12'  " +
                " END AS 'week', " +
                " IFNULL( datas.totalUser, 0 ) totalUser  " +
                "FROM " +
                " ( " +
                " SELECT " +
                "  a.d weeks, " +
                "  b.totalUser  " +
                " FROM " +
                "  ( " +
                "  SELECT " +
                "   1 AS d UNION ALL " +
                "  SELECT " +
                "   2 UNION ALL " +
                "  SELECT " +
                "   3 UNION ALL " +
                "  SELECT " +
                "   4 UNION ALL " +
                "  SELECT " +
                "   5 UNION ALL " +
                "  SELECT " +
                "   6 UNION ALL " +
                "  SELECT " +
                "   7 UNION ALL " +
                "  SELECT " +
                "   8 UNION ALL " +
                "  SELECT " +
                "   9 UNION ALL " +
                "  SELECT " +
                "   10 UNION ALL " +
                "  SELECT " +
                "   11 UNION ALL " +
                "  SELECT " +
                "   12  " +
                "  ) a " +
                "  LEFT JOIN ( " +
                "  SELECT " +
                "   date_format( us.constructionTime, '%m' ) AS datetime, " +
                "   COUNT( us.id ) AS totalUser  " +
                "  FROM " +
                "   t_patrolaccount us  " +
                "  WHERE " +
                " status<>'2' and proId='" + sysUser.getProId() + "' and enterpriseId=' " + sysUser.getEnterprise().getId() + "'and areaId='" + enterpriseProject.getId() + "' and receiverId='" + sysUser.getId() + "'" +
                "  and YEAR ( us.constructionTime )= YEAR ( " +
                "   NOW())  " +
                "  GROUP BY " +
                "   datetime  " +
                "  ) b ON a.d = b.datetime  " +
                " ) datas  " +
                "ORDER BY " +
                " datas.weeks ASC").list();
        List<Object[]> list7 = patrolAccountService.getHibernateSession().createSQLQuery(
                "SELECT CASE datas.weeks  " +
                        "  WHEN '1' THEN '1' WHEN '2' THEN " +
                        "  '2' WHEN '3' THEN '3' WHEN '4' THEN '4' WHEN '5' THEN '5'  " +
                        "  WHEN '6' THEN '6' WHEN '7' THEN '7' WHEN '8' THEN '8'  " +
                        "  WHEN '9' THEN '9' WHEN '10' THEN '10' WHEN '11' THEN " +
                        "  '11' WHEN '12' THEN '12' END AS 'week', " +
                        " IFNULL( datas.totalUser, 0 ) totalUser FROM ( " +
                        " SELECT a.d weeks, b.totalUser FROM ( " +
                        " SELECT 1 AS d UNION ALL " +
                        " SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL " +
                        " SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL " +
                        " SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL " +
                        " SELECT 11 UNION ALL SELECT 12) a LEFT JOIN (SELECT date_format( us.publishTime, '%m' ) AS datetime, " +
                        " COUNT( us.id ) AS totalUser FROM t_patrolaccount us WHERE " +
                " status='2' and proId='" + sysUser.getProId() + "' and enterpriseId='" + sysUser.getEnterprise().getId() + "'and areaId='" + enterpriseProject.getId() + "' and receiverId='" + sysUser.getId() + "'" +
                "  and YEAR ( us.publishTime )= YEAR ( " +
                "   NOW())  " +
                "  GROUP BY " +
                "   datetime  " +
                "  ) b ON a.d = b.datetime  " +
                " ) datas  " +
                "ORDER BY " +
                " datas.weeks ASC").list();
        String[] data1 = new String[list6.size()];
        String[] month = new String[]{"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
        HashMap<String, String[]> map = new HashMap<>();
        String[] data2 = new String[list7.size()];
        for (int i = 0; i < list6.size(); i++) {
            data1[i] = list6.get(i)[1].toString();
        }
        for (int i = 0; i < list7.size(); i++) {
               data2[i] = list7.get(i)[1].toString();
        }
        map.put("month", month);
        //已完成
        map.put("com", data1);
        //未完成
        map.put("unCom", data2);
        msgInfo.setData(map);
        return msgInfo;
    }

    @ResponseBody
    @RequestMapping("unCompleteList")
    public Layui unCompleteList(){
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        EnterpriseProject enterpriseProject = (EnterpriseProject) SecurityUtils.getSubject().getSession().getAttribute("area");
        DetachedCriteria criteria = DetachedCriteria.forClass(PatrolAccount.class);
        criteria.add(Restrictions.eq("status","2"));
        criteria.add(Restrictions.eq("receiverId",sysUser.getId()+""));
        criteria.add(Restrictions.eq("enterpriseId",sysUser.getEnterprise().getId()));
        criteria.add(Restrictions.eq("areaId",enterpriseProject.getId()));
        criteria.add(Restrictions.eq("proId",sysUser.getProId()));
        List<PatrolAccount> list = patrolAccountService.findByConditions(criteria);
        return Layui.data(0, "success", patrolAccountService.getTotalCount(), list);
    }


}
